package de.wwi11b2.mjp.comicvineclient;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ItemActivity extends Activity {

	private TextView itemNameTextView = null;
	private TextView itemDescriptionNameTextView = null;
	private FrameLayout image_frame = null;
	private Item item = null;
	private Button itemSaveButton = null;
	private Button itemDeleteButton = null;
	private ImageButton imageButtonHome = null;
	private DataHelper dataHelper = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_item);
		dataHelper = new DataHelper(getApplicationContext());
		itemNameTextView = (TextView) findViewById(R.id.itemNameTextView);
		itemDescriptionNameTextView = (TextView) findViewById(R.id.itemDescriptionTextView);
		image_frame = (FrameLayout) findViewById(R.id.image_frame);
		imageButtonHome = (ImageButton) findViewById(R.id.imageButtonHome);
		imageButtonHome.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), MainActivity.class);
				startActivity(intent);
			}
		});
		itemSaveButton = (Button) findViewById(R.id.itemSaveButton);
		itemSaveButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (dataHelper.save(item)) {
					Toast.makeText(getApplicationContext(),
							"Item " + item.getName() + " saved!", Toast.LENGTH_LONG)
							.show();
					itemSaveButton.setEnabled(false);
				} else {
					Toast.makeText(getApplicationContext(),
							"Oops! Something went terribly wrong!", Toast.LENGTH_LONG)
							.show();
				}
			}

		});
		itemSaveButton.setEnabled(false);
		itemDeleteButton = (Button) findViewById(R.id.itemDeleteButton);
		itemDeleteButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (dataHelper.delete(item)) {
					Toast.makeText(getApplicationContext(),
							"Item " + item.getName() + " deleted!", Toast.LENGTH_LONG)
							.show();
					Intent intent = new Intent();
					intent.setClass(getApplicationContext(), DatabaseActivity.class);
					startActivity(intent);
				} else {
					Toast.makeText(getApplicationContext(),
							"Oops! Something went terribly wrong!", Toast.LENGTH_LONG)
							.show();
				}
			}

		});
		itemDeleteButton.setEnabled(false);
	}

	@Override
	protected void onResume() {
		super.onResume();
		boolean isFromSearch = getIntent().getBooleanExtra("isFromSearch", true);
		if(isFromSearch){
			itemSaveButton.setEnabled(true);
		}else{
			itemDeleteButton.setEnabled(true);
		}
		item = (Item) getIntent().getExtras().getSerializable("item");
		if (item != null) {
			itemNameTextView.setText(item.getName());
			itemDescriptionNameTextView.setText(item.getDescription());
			ImageView image = new ImageView(getApplicationContext());
			Bitmap bitmap = BitmapFactory.decodeByteArray(item.getImage(), 0,
					item.getImage().length);
			image.setImageBitmap(bitmap);
			image_frame.addView(image);
		} else {
			TextView noResultsTextView = new TextView(getApplicationContext());
			noResultsTextView.setText("No result found!");
			noResultsTextView.setTextAppearance(getApplicationContext(),
					android.R.style.TextAppearance_Large);
			noResultsTextView.setTextColor(Color.BLACK);
			image_frame.addView(noResultsTextView);
		}
	}

}
